package com.paas.form.core.model;

import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.jsContent.ScriptContent;
import lombok.Data;

import java.util.Collections;


@Data
public class DialogButton implements FormItem {


    private int indentNum;

    public static final String DIALOG_BUTTON_METHODS= "            /**\n" +
            "             * 提交-保存\n" +
            "             */\n" +
            "            saveForm() {\n" +
            "                // 表单校验\n" +
            "                this.$refs.${formDataModel.formRef}.validate((valid) => {\n" +
            "                    if (valid) {\n" +
            "                        // 保存访问地址配置\n" +
            "\t\t\t\t\t\tlet saveInfo ={};\n" +
            "\t\t\t\t\t\tfor(let key in this.${formDataModel.formModel}){\n" +
            "\t\t\t\t\t\t\tif(this.${formDataModel.formModel}[key] instanceof Array){\n" +
            "\t\t\t\t\t\t\t\tsaveInfo[key] = JSON.stringify(this.${formDataModel.formModel}[key]);\n" +
            "\t\t\t\t\t\t\t}else {\n" +
            "\t\t\t\t\t\t\t\tsaveInfo[key] = this.${formDataModel.formModel}[key];\n" +
            "\t\t\t\t\t\t\t}\n" +
            "\t\t\t\t\t\t}\n" +
            "                        ${className}Service.save(saveInfo).then(() => {\n" +
            "                            this.$message({message: \"保存成功\", type: \"success\"});\n" +
            "                            this.$emit(\"refreshDataList\", 1);\n" +
            "                            this.handleClose();\n" +
            "                        });\n" +
            "                    }\n" +
            "                });\n" +
            "            },\n" +
            "\n" +
            "            /**\n" +
            "             * 关闭弹出框\n" +
            "             */\n" +
            "            handleClose() {\n" +
            "                this.${formDataModel.formModel} = {};\n" +
            "                this.$refs.${formDataModel.formRef}.resetFields();\n" +
            "                this.showDialog = false;\n" +
            "            },";

    public static final String DIALOG_BUTTON_HTML ="" +
            "        <div slot=\"footer\" class=\"dialog-footer\">\n" +
            "            <el-button type=\"primary\" v-if=\"!isdetail\" @click=\"saveForm\">保存</el-button>\n" +
            "            <el-button @click=\"handleClose\">返回</el-button>\n" +
            "        </div>";


    public String getDialogButtonHtml(){
        String dialogButtonHtmlStr=String.join("", Collections.nCopies(this.indentNum, "\t")) + "<div slot=\"footer\" class=\"dialog-footer\">\n";
        dialogButtonHtmlStr =dialogButtonHtmlStr + String.join("", Collections.nCopies(this.indentNum+1, "\t")) + "<el-button type=\"primary\" @click=\"saveForm\">保存</el-button>\n";
        dialogButtonHtmlStr =dialogButtonHtmlStr +  String.join("", Collections.nCopies(this.indentNum+1, "\t")) + "<el-button @click=\"handleClose\">返回</el-button>\n";
        dialogButtonHtmlStr=dialogButtonHtmlStr + String.join("", Collections.nCopies(this.indentNum, "\t")) + "</div>\n";
        return dialogButtonHtmlStr;

    }





    public DialogButton(int indentNum){
        this.indentNum = indentNum;
    }


    @Override
    public String makeVue() {
        return getDialogButtonHtml();
    }

    @Override
    public ScriptContent makeJs() {
        ScriptContent scriptContent= new ScriptContent();
        scriptContent.setMethods(DIALOG_BUTTON_METHODS);
        return scriptContent;
    }
}
